package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.StringUtil;
import com.ykt.common.utils.UB;

import java.util.List;
import java.util.Map;

/**
 * 用户或商户
 */
public class User extends BaseModel<User> {
    private static final long serialVersionUID = 1L;

    public final static String name = "name";
    public final static String password = "password";
    public final static String phone = "phone";
    public final static String head = "head";
    public final static String email = "email";
    public final static String role = "role";
    public final static String status = "status";

    public final static User dao = new User();


    public String getPassword() {
        return getStr(password);
    }

    public String getPhone() {
        return getStr(phone);
    }

    public String getHead() {
        return getStr(head);
    }

    public String getEmail() {
        return getStr(email);
    }

    public String getRole() {
        return getStr(role);
    }

    public int getStatus() {
        return UB.i(getInt(status), -1);
    }


    /**
     * 用户角色
     */
    public enum Type {
        /**
         * 普通用户
         */
        CUSTOMER,
        /**
         * 商户
         */
        MERCHANT,
        /**
         * 全部包含为
         */
        ALL
    }

    //cms查询用户
    @Override
    public Page<User> paginate(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT u.* ";
        String exceptSelect = " FROM user u ";
        paramMap.put("EQ_u.role", "CUSTOMER");
        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }

    //cms查询商户
    public Page<User> pageFindModelsForMerchant(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT u.* ";
        String exceptSelect = " FROM user u ";
        paramMap.put("EQ_u.role", "MERCHANT");
        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }

    public User findByPhone(String phone) {
        return findFirst("select * from user where role='CUSTOMER' and phone=? limit 1", phone);
    }
    
    public User findMerchantByPhone(String phone) {
        return findFirst("select * from user where role='MERCHANT' and phone=? limit 1", phone);
    }

    public List<User> findByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return null;
        }

        String sql = "select * from user where id in (" + StringUtil.join(ids, ",") + ")";
        return find(sql);
    }

}